package com.example.houduan.demos.web.controller;

import com.example.houduan.demos.web.entity.Department;
import com.example.houduan.demos.web.entity.Employee;
import com.example.houduan.demos.web.mapper.DepartMapper;
import com.example.houduan.demos.web.vo.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/depart")
public class DepartmentController {
    @Resource
    private DepartMapper departMapper;
    @GetMapping("/getAll")
    public List<Department> getAll(){
        return departMapper.findAll();
    }

    @GetMapping("/getWithChild")
    public List<DepartmentVo> getWithChild(){
        List<DepartmentVo> departList = new ArrayList<>();
        List<Department> departMapperAll = departMapper.findAll();
        if (departMapperAll != null){
            for( Department department : departMapperAll){
                DepartmentVo departmentVo = new DepartmentVo();
                BeanUtils.copyProperties(department,departmentVo);
                List<Employee> employees = departMapper.getWith(department.get_id());
                if (employees != null){
                    departmentVo.setEmployees(employees);
                }
                departList.add(departmentVo);
            }
        }
        return departList;
    }
}
